package com.kh.first.controller;

import com.kh.first.pojo.KHDevicePojo;
import com.kh.first.pojo.common.AjaxJson;
import com.kh.first.pojo.DeviceUseLogPojo;
import com.kh.first.pojo.common.LayTableJson;
import com.kh.first.pojo.common.PageParam;

import com.kh.first.service.DeviceUseLogService;
import com.kh.first.service.KHDeviceService;
import com.kh.first.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("deviceUseLogController")
public class DeviceUseLogController {
    static final String currentFolder = UserUtils.THEME + "deviceuselog/";
    @Autowired
    private DeviceUseLogService deviceUseLogService;

    @Autowired
    private KHDeviceService kHDeviceService;

    @RequestMapping(params = "goDeviceUseLogList")
    public String goDeviceUseLogList() {
    return currentFolder + "deviceuselog-list";
    }


    @RequestMapping(params = "dataGrid")
    @ResponseBody
    public LayTableJson<DeviceUseLogPojo> dataGrid(DeviceUseLogPojo pojo, PageParam pageParam) {
        LayTableJson<DeviceUseLogPojo> ajaxJson = new LayTableJson<>();
        int limit = pageParam.getLimit();
        int page = pageParam.getPage();
        int size = limit == 0 ? 10 : limit;
        pojo.setStart(page == 0 ? 0 : (page - 1) * size);
        pojo.setSize(size);
        List<DeviceUseLogPojo> DeviceUseLogList = deviceUseLogService.getDeviceUseLogList(pojo);
        ajaxJson.data = DeviceUseLogList;
        ajaxJson.count = deviceUseLogService.countDeviceUseLogList(pojo).intValue();
        return ajaxJson;
    }

    @RequestMapping(params = "goDeviceUseLogAdd")
    public String goDeviceUseLogAdd(Long id,Model model) {
        KHDevicePojo pojo=new KHDevicePojo();
        pojo.setId(id);
        List<KHDevicePojo> khDeviceList = kHDeviceService.getKHDeviceList(pojo);
        KHDevicePojo devicePojo = khDeviceList.get(0);
        model.addAttribute("deviceuselog", devicePojo);
        return currentFolder + "deviceuselog-add-update";
    }

    @RequestMapping(params = "goDeviceUseLogQuery")
    public String goDeviceUseLogQuery(Model model,DeviceUseLogPojo pojo){
        model.addAttribute("deviceuselog", pojo);
        return currentFolder +"deviceuselog-query";
    }

    @RequestMapping(params = "goDeviceUseLogUpdate")
    public String goDeviceUseLogUpdate(DeviceUseLogPojo deviceuselog, Model model) {
        List<DeviceUseLogPojo> DeviceUseLogList = deviceUseLogService.getDeviceUseLogList(deviceuselog);
        deviceuselog = DeviceUseLogList.get(0);//报错让他报
        model.addAttribute("deviceuselog",deviceuselog);
        return currentFolder + "deviceuselog-add-update";
    }

    @RequestMapping(params = "doDeviceUseLogAdd")
    @ResponseBody
    public AjaxJson<String> doUserAdd(DeviceUseLogPojo deviceuselog) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        deviceUseLogService.saveDeviceUseLog(deviceuselog);
        KHDevicePojo devicePojo=new KHDevicePojo();
        devicePojo.setId(deviceuselog.getDeviceId());
        List<KHDevicePojo> khDeviceList = kHDeviceService.getKHDeviceList(devicePojo);
        devicePojo = khDeviceList.get(0);
        devicePojo.setUseUser(deviceuselog.getAcceptUser());
        kHDeviceService.updateKHDeviceById(devicePojo);
        return ajaxJson;
    }
    @RequestMapping(params = "doDeviceUseLogUpdate")
    @ResponseBody
    public AjaxJson<String> doDeviceUseLogUpdate(DeviceUseLogPojo deviceuselog) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        deviceUseLogService.updateDeviceUseLogById(deviceuselog);
        return ajaxJson;
    }

}
